package com.gzh.system.service.impl;

import com.gzh.core.dao.mapper.FileMapper;
import com.gzh.core.dao.po.FileEntity;
import com.gzh.core.util.CosUtils;
import com.gzh.core.util.NumericHelper;
import com.gzh.system.service.CosService;
import com.gzh.system.service.dto.ImgIn;
import com.gzh.system.service.dto.ImgOut;
import com.gzh.system.service.dto.UploadIn;
import com.gzh.system.service.dto.UploadOut;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Condition;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service
@Slf4j
public class CosServiceImpl implements CosService {

  @Autowired
  private FileMapper fileMapper;

  @Transactional
  public UploadOut upload(UploadIn in) {
    UploadOut out = new UploadOut();
    MultipartFile multipartFile = in.getFile();
    String bucketName = in.getBucketName();
    if (multipartFile.isEmpty()) {
      return null;
    }
    String path = CosUtils.upload(multipartFile, bucketName, in.getPrePath());
    FileEntity fileEntity = new FileEntity();
    fileEntity.setBucketName(bucketName);
    fileEntity.setPath(path);
    fileEntity.setFileName(multipartFile.getOriginalFilename());
    fileEntity.setClassify(in.getClassify());
    fileMapper.insertSelective(fileEntity);
    out.setFileId(fileEntity.getFileId());
    out.setImgUrl(path);
    return out;
  }

  @Override
  public void downLoad(String fileId, HttpServletResponse response) {
    FileEntity fileEntityQuery = new FileEntity();
    fileEntityQuery.setFileId(NumericHelper.toInteger(fileId));
    FileEntity fileEntity = fileMapper.selectOne(fileEntityQuery);
    String path = fileEntity.getPath();
    CosUtils.downLoad(fileEntity.getBucketName(),path,fileEntity.getFileName(),response);
  }

  @Override
  public void deleteFile(String fileId) {
    Condition fileCondition = new Condition(FileEntity.class);
    fileCondition.createCriteria().andEqualTo("fileId",fileId);
    List<FileEntity> fileEntityList = fileMapper.selectByCondition(fileCondition);
    if (!CollectionUtils.isEmpty(fileEntityList)){
      CosUtils.delete(fileEntityList.get(0).getBucketName(),fileEntityList.get(0).getPath());
      fileMapper.deleteByCondition(fileCondition);
    }
  }

  @Override
  public List<ImgOut> getImg(ImgIn in) {
    List<ImgOut> outList = new ArrayList<>();
    Condition fileCondition = new Condition(FileEntity.class);
    fileCondition.createCriteria().andEqualTo("classify", in.getKey());
    List<FileEntity> fileEntities = fileMapper.selectByCondition(fileCondition);
    if (fileEntities.size() > 4) {
      fileEntities = fileEntities.subList(0, 4);
    }
    for (FileEntity fileEntity : fileEntities) {
      ImgOut imgOut = new ImgOut();
      String objectUrl = CosUtils.getImg(fileEntity.getBucketName(), fileEntity.getPath());
      imgOut.setImgUrl(objectUrl);
      imgOut.setFileId(fileEntity.getFileId());
      outList.add(imgOut);
    }
    return outList;
  }
}
